package com.scorpio.mqtt.service.impl;

import com.scorpio.mqtt.constant.MQConstant;
import com.scorpio.mqtt.service.MqttService;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.jms.TextMessage;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

@Service
public class MqttServiceImpl implements MqttService {

    @Autowired
    @Qualifier(MQConstant.ORDER_OVERDUE)
    private ActiveMQQueue orderOverdueQueue;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public void pushOrderOverdueQueu(String orderOn,long time) {
        jmsMessagingTemplate.convertAndSend(orderOverdueQueue, "orderOn");
    }

    @Override
    public void timingGo(ActiveMQQueue queue, String data, long time) {
        jmsTemplate.send(queue, session -> {
            TextMessage message = session.createTextMessage();
            message.setText(data);
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
            return message;
        });
    }
}
